package fr.teuteuf.runstickmanrun.map.block;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.World;

import fr.teuteuf.runstickmanrun.map.MapGenerator;
import fr.teuteuf.runstickmanrun.map.tile.DamagingBoxTile;
import fr.teuteuf.runstickmanrun.map.tile.SolidBoxTile;
import fr.teuteuf.runstickmanrun.map.tile.Tile;

public class StairsAndPlatformsBlock extends AbstractMapBlock {
	
	public StairsAndPlatformsBlock(int difficultyLevel) {
		setDifficulties(3, 4, 6);
		setCurrentLevel(difficultyLevel);
	}

	@Override
	public int generateBlock(World world, MapGenerator mg, int nbHorizontalTiles) {
		int stairsNumber = MathUtils.random(8, 15);
		int holeSize;
		int blockLength = 0;
		int holesNumber = MathUtils.random(3, 10);
		
		if(getCurrentLevel() >= getMaxDifficultyLevel()) {
			holeSize = MathUtils.random(2, 9);
		} else {
			holeSize = MathUtils.random(2, 8);
		}
		
		//build the stairs
		mg.addTile(new SolidBoxTile((byte) 13, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (1 + 3) * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
		if(getCurrentLevel() >= getMedDifficultyLevel()) {
			mg.addTile(new DamagingBoxTile(2, 8, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 1 * Tile.TILE_SIZE, world, 10));
			mg.addTile(new DamagingBoxTile(2, 8, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 2 * Tile.TILE_SIZE, world, 10));
			mg.addTile(new DamagingBoxTile(2, 8, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 3 * Tile.TILE_SIZE, world, 10));
		}
		blockLength++;
		mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (2 + 3) * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 4, 1, 2, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (1 + 3) * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
		blockLength++;
		for(int i = 3; i < stairsNumber; i++) {
			if(i == stairsNumber - 1) {
				mg.addTile(new SolidBoxTile((byte) 11, 7, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (i + 3) * Tile.TILE_SIZE, world));
				buildBlockColumn(5, (byte) 2, (nbHorizontalTiles + blockLength), 5, i + 2, 3, 9, world, mg);
				mg.addTile(new SolidBoxTile((byte) 6, 1, 2, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (1 + 3) * Tile.TILE_SIZE, world));
			} else {
				mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (i + 3) * Tile.TILE_SIZE, world));
				buildBlockColumn(0, (byte) 0, (nbHorizontalTiles + blockLength), 5, i + 2, 1, 2, world, mg);
				mg.addTile(new SolidBoxTile((byte) 4, 1, 2, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (1 + 3) * Tile.TILE_SIZE, world));
			}

			mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
			blockLength++;
		}
		
		//build plateforms
		for(int i = 1; i < holesNumber; i++) {
			for(int j = 0; j < holeSize; j++) {
				mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
				blockLength++;
			}
			
			if(getCurrentLevel() < getMaxDifficultyLevel()) {
				mg.addTile(new SolidBoxTile((byte) 13, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (stairsNumber + 2) * Tile.TILE_SIZE, world));
				mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
				blockLength++;
				mg.addTile(new SolidBoxTile((byte) 7,  2, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (stairsNumber + 2) * Tile.TILE_SIZE, world));
				mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
				blockLength++;
			} else {
				mg.addTile(new SolidBoxTile((byte) 11, 7, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (stairsNumber + 2) * Tile.TILE_SIZE, world));
				mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
				blockLength++;
			}
		}
		
		//build wall
		for(int j = 0; j < holeSize; j++) {
			mg.addTile(new SolidBoxTile((byte)  1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE,       0 * Tile.TILE_SIZE, world));
			blockLength++;
		}
		mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (stairsNumber + 2) * Tile.TILE_SIZE, world));
		buildBlockColumn(5, (byte) 8, (nbHorizontalTiles + blockLength), 1, stairsNumber + 1, 2, 9, world, mg);
		mg.addTile(new Tile(1, 2, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE));
		blockLength++;
		mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (stairsNumber + 2) * Tile.TILE_SIZE, world));
		buildBlockColumn(0, (byte) 0, (nbHorizontalTiles + blockLength), 0, stairsNumber + 1, 1, 2, world, mg);
		blockLength++;
		mg.addTile(new SolidBoxTile((byte) 3, 2, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (stairsNumber + 2) * Tile.TILE_SIZE, world));
		buildBlockColumn(0, (byte) 2, (nbHorizontalTiles + blockLength), 1, stairsNumber + 1, 1, 2, world, mg);
		mg.addTile(new Tile(1, 2, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE));
		blockLength++;
		
		return nbHorizontalTiles + blockLength;
	}

}
